package com.kylin.struts;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.actions.DispatchAction;

import com.kylin.common.Constants;
import com.kylin.common.entity.MsgKey;
import com.kylin.common.entity.UserCache;
import com.kylin.common.exception.BusinessException;
import com.kylin.common.util.MemObjFactory;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.QueryInfo;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletResponse;

public abstract class BaseDispatchAction extends DispatchAction {
	protected static Logger log = Logger.getLogger(BaseDispatchAction.class);
	
		/* 
	 * added by mjj 
	 *  
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward unspecified(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response){
		String forward ="default";
		return mapping.findForward(forward);
		
	}
	 /*
	  * added by mjj
	  *  
	  * @param mapping
	  * @param form
	  * @param request
	  * @param response
	  * @return
	  * @throws Exception
	  */
	public ActionForward execute(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response)
			throws Exception{
		
		log.info(request.toString());
		try{
			return super.execute(mapping, form, request, response);
		}
		catch (Exception e){
			return mapping.findForward("system");
		}
	}
	
	
	/**
	 * 统一异常处理
	 * @param request
	 * @param e
	 * @param forword
	 * @return
	 */
	public String doException(HttpServletRequest request,Exception e,String forword){
		log.error(e);
		e.printStackTrace();
		if(e instanceof BusinessException == false){
			forword = "SystemException";
		}
		String err = e.toString();
		if(e.getCause() != null)
			err = e.getCause().getMessage();
		
        request.setAttribute(Constants.MSGKEY, new MsgKey(e.getMessage(),err));
        request.setAttribute(Constants.KEY_REQUEST_EXCEPTION, e);
		return forword;
	}
	

    public UserCache getUserCache(HttpServletRequest request){
		HttpSession session =  request .getSession();
		UserCache cache = (UserCache)session.getAttribute(Constants.KEY_SESSION_USERCACHE);
		if(cache == null){
			cache = new UserCache();
			session.setAttribute(Constants.KEY_SESSION_USERCACHE, cache);
			log.info("\n[INIT] First create User Cache");
		}
    	return cache;
    }
    
    public QueryInfo getUserCacheQueryInfo(HttpServletRequest request, String queryId){
    	return getUserCache(request).getQueryInfo(queryId);
    }
    
    public String addUserCacheQueryInfo(HttpServletRequest request,  QueryInfo queryInfo){
    	return getUserCache(request).addQueryInfo(queryInfo);
    }
    
    public CriteriaInfo getUserCacheCriteriaInfo(HttpServletRequest request, String searchId){
    	return getUserCache(request).getCriteriaInfo(searchId);
    }
    
    public String addUserCacheCriteriaInfo(HttpServletRequest request,  CriteriaInfo criteriaInfo){
    	return getUserCache(request).addCriteriaInfo(criteriaInfo);
    }
    
    public void addCacheObject(HttpServletRequest request,String key,Object obj){
    	getUserCache(request).addObject(key, obj);
    }
    public Object getCacheObject(HttpServletRequest request,String key){
    	return getUserCache(request).getObject(key);
    }
    
    public Locale getSessionLocale(HttpServletRequest request){
    	Locale loc=(Locale)request.getSession().getAttribute(org.apache.struts.Globals.LOCALE_KEY);
    	String locStr = loc.toString();
    	if(locStr.length() < 3){
    		for(int i=0;i<MemObjFactory.SysLocales.length ;i++){
		        String lv = MemObjFactory.SysLocales[i].toString();
		        if( lv.startsWith(loc.toString().substring(0,2) ) ){
		            loc = MemObjFactory.SysLocales[i];
		        }
		        if(locStr.equalsIgnoreCase(lv) ){
		            loc = MemObjFactory.SysLocales[i];
		            break;
		        }
		    } 
    		request.getSession().setAttribute(org.apache.struts.Globals.LOCALE_KEY,loc);
    	}
	    return loc;
    }


    public boolean hasParam(String paraValue){
		if(paraValue == null)
			return false;
		if(paraValue.equals(""))
			return false;
		return true;
	}

}
